package com.crscd.jdbc.senior.dao.impl;

import com.crscd.jdbc.advanced.pojo.Employee;
import com.crscd.jdbc.senior.dao.BaseDao;
import com.crscd.jdbc.senior.dao.EmployeeDao;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 10/23/25 11:43 AM
 * @modifiedBy:
 * @version: 1.0
 */
public class EmployeeDaoImpl extends BaseDao implements EmployeeDao {
  private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeDaoImpl.class);

  @Override
  public List<Employee> selectAll() {
    final String sql =
        "select emp_id as empId, emp_name as empName, emp_salary as empSalary, emp_age as empAge from t_emp";
    List<Employee> employeeList = Collections.emptyList();
    try {
      employeeList = executeQuery(Employee.class, sql);
    } catch (SQLException
        | NoSuchMethodException
        | InvocationTargetException
        | InstantiationException
        | IllegalAccessException
        | NoSuchFieldException e) {
      LOGGER.error("execute query meet error.", e);
    }
    return employeeList;
  }

  @Override
  public Employee selectByEmpId(Integer empId) {
    final String sql =
        "select emp_id as empId, emp_name as empName, emp_salary as empSalary, emp_age as empAge from t_emp "
            + "where emp_id=?";
    Employee employee = null;
    try {
      employee = executeQueryBean(Employee.class, sql, empId);
    } catch (SQLException
        | NoSuchMethodException
        | InvocationTargetException
        | InstantiationException
        | IllegalAccessException
        | NoSuchFieldException e) {
      LOGGER.error("execute query meet error.", e);
    }
    return employee;
  }

  @Override
  public int insert(Employee employee) {
    final String sql = "insert into testdb.t_emp (emp_name, emp_salary, emp_age) values (?, ?, ?)";
    try {
      return executeUpdate(
          sql, employee.getEmpName(), employee.getEmpSalary(), employee.getEmpAge());
    } catch (SQLException e) {
      LOGGER.error("execute insert meet error.", e);
    }
    return 0;
  }

  @Override
  public int update(Employee employee) {
    final String sql = "update testdb.t_emp set emp_salary=? where emp_id=?";
    try {
      return executeUpdate(sql, employee.getEmpSalary(), employee.getEmpId());
    } catch (SQLException e) {
      LOGGER.error("execute update meet error.", e);
    }
    return 0;
  }

  @Override
  public int deleteByEmpId(Integer empId) {
    final String sql = "delete from testdb.t_emp where emp_id=?";
    try {
      return executeUpdate(sql, empId);
    } catch (SQLException e) {
      LOGGER.error("execute delete meet error.", e);
    }
    return 0;
  }
}
